package com.dev;
import android.content.ClipData;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fb.FileBrowserPage;
import java.io.File;
public class ShowFileDetails extends LinearLayout implements OnClickListener,Runnable {
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	LinearLayout titlebar;
	IconViewClose close;
	TextView title;
	TextView msg;
	File destination;
	ColorTextButton btCopy;
	ColorTextButton btCopyAll;
	MainActivity ma;
	public ShowFileDetails(MainActivity ma) {
		super(ma);
		this.ma = ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		title = new TextView(ma);
		title.setTextColor(0xff000000);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setPadding(9, 0, 0, 0);
		title.setText("详情");
		close = new IconViewClose(ma);
		close.setOnClickListener(this);
		titlebar = new LinearLayout(ma){
			public void onDraw(Canvas cvs) {
				super.onDraw(cvs);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				cvs.drawRect(0, getHeight() - FileBrowserPage.borderWidth, getWidth(), getHeight(), p);
			}
		};
		titlebar.setWillNotDraw(false);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(title, new LayoutParams(0, -1, 1));
		titlebar.addView(close);
		msg = new TextView(ma);
		msg.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 13);
		msg.setTypeface(Typeface.MONOSPACE);
		msg.setTextColor(0xff000000);
		msg.setPadding(10, 0, 0, 0);
		msg.setMovementMethod(LinkMovementMethod.getInstance());
		msg.setLinksClickable(true);
		btCopy=new ColorTextButton(ma);
		btCopy.setText(" 复制路径 ");
		btCopy.setOnClickListener(this);
		btCopyAll=new ColorTextButton(ma);
		btCopyAll.setText(" 复制全路径 ");
		btCopyAll.setOnClickListener(this);
		LinearLayout bts=new LinearLayout(ma);
		bts.setOrientation(HORIZONTAL);
		bts.addView(btCopy);
		bts.addView(btCopyAll);
		addView(titlebar, -1, (int)(30 * ma.dp));
		addView(msg, -1, -2);
		addView(bts,-1,-2);
	}
	FileBrowserPage fbp;
	public void show(File destination,FileBrowserPage fbp) {
		this.destination = destination;
		this.fbp=fbp;
		wlp.width = ma.gui.getWidth() * 3 / 4;
		wlp.height = -2;
		String path;
		if(destination==null){
			path=fbp.currentDirectory.getAbsolutePath();
			SpannableStringBuilder ssb=new SpannableStringBuilder("Dir:"+path+'\n');
			ssb.setSpan(new GotoSpan(),4,path.length()+4,0);
			long size=0;
			int dir=0;
			int file=0;
			for(int index=0,len=fbp.willshow.length;index<len;index++){
				if(fbp.selected[index]){
					File f=fbp.willshow[index];
					if(f.isFile()){
						size+=f.length();
						file++;
					}else dir++;
				}
			}
			ssb.append("总大小："+"≥"+size+'\n');
			ssb.append("文件："+file+"个"+'\n');
			ssb.append("目录："+dir+"个"+'\n');
			msg.setText(ssb);
		}else{
			path=destination.getAbsolutePath();
			String infoCanRead="是否可读：";
			String infoCanWrite="是否可写：";
			if (destination.isFile()) {
				StringBuilder sb=new StringBuilder();
				String infoSize;
				String str2="路径:" + path + "\n";
				long length=destination.length();
				String size;
				infoSize="大小：" + destination.length() + "B"+'\n';
				if(length>1024){
					if(length>1024*1024){
						size=length/1024/1024+"MB";
						infoSize+="("+size+")";
					}else{
						size=length/1024+"KB";
						infoSize+="("+size+")";
					}
				}
				if (destination.canRead()) {
					sb.append(infoSize);
					sb.append(str2);
					sb.append(infoCanRead+"true"+'\n');
					sb.append(infoCanWrite+destination.canWrite());
					SpannableString ss=new SpannableString(sb);
					ss.setSpan(new GotoSpan(), infoSize.length() + 3, infoSize.length() + 3 + path.length(), 0);
					msg.setText(ss);
				} else {
					sb.append(infoSize);
					sb.append(str2);
					sb.append(infoCanRead+"false"+'\n');
					sb.append(infoCanWrite+destination.canWrite());
					SpannableString ss=new SpannableString(sb);
					ss.setSpan(new GotoSpan(), infoSize.length() + 3, infoSize.length() + 3+ path.length(), 0);
					msg.setText(ss);
				}
			} else if (destination.canRead()) {
				String a="类型：" + "目录" + '\n';
				String b="路径:" + path + "\n";
				SpannableStringBuilder ss=new SpannableStringBuilder();
				ss.append(a);
				ss.append(b);
				ss.append(infoCanRead+"true"+'\n');
				ss.append(infoCanWrite+destination.canWrite()+'\n');
				ss.setSpan(new GotoSpan(), a.length() + 3, a.length() + 3 + path.length(), 0);
				msg.setText(ss);
			} else {
				SpannableStringBuilder sss=new SpannableStringBuilder();
				String a="路径:" + path + '\n';
				sss.append(a);
				sss.append(infoCanRead+"false"+'\n');
				sss.append(infoCanWrite+destination.canWrite());
				sss.setSpan(new GotoSpan(), 3, 3 + path.length(), 0);
				msg.setText(sss);
			}
		}
		ma.wm.addView(this, wlp);
	}
	void dismiss() {
		ma.wm.removeView(this);
	}
	@Override
	public void run() {
	}
	@Override
	public void onClick(View v) {
		if (v == close) {
			dismiss();
		}else if(v==btCopy){
			ClipData cd=ClipData.newPlainText(MainActivity.CLIPBOARD_TAG,destination.getParent());
			ma.cm.setPrimaryClip(cd);
			Toast.makeText(ma,"已复制路径",0).show();
		}else if(v==btCopyAll){
			ClipData cd=ClipData.newPlainText(MainActivity.CLIPBOARD_TAG, destination.getAbsolutePath());
			ma.cm.setPrimaryClip(cd);
			Toast.makeText(ma, "已复制全路径", 0).show();
		}
	}
	@Override
	protected void onDraw(Canvas canvas) {
		Paint p=new Paint();
		p.setColor(FileBrowserPage.borderColor);
		p.setStyle(Paint.Style.FILL);
		canvas.drawColor(0xffffffff);
		canvas.drawRect(0, 0, FileBrowserPage.borderWidth, getHeight(), p);
		canvas.drawRect(0, 0, getWidth(), FileBrowserPage.borderWidth, p);
		canvas.drawRect(getWidth() - FileBrowserPage.borderWidth, 0, getWidth(), getHeight(), p);
		canvas.drawRect(0, getHeight() - FileBrowserPage.borderWidth, getWidth(), getHeight(), p);
		super.onDraw(canvas);
	}
	final class GotoSpan extends ClickableSpan {
		@Override
		public void onClick(View view) {
			File dir=destination.getParentFile();
			if(dir.canRead()){
				for(int index=0,count=ma.gui.tabs.getChildCount();;index++){
					if(index<count){
						View got=ma.gui.tabs.getChildAt(index);
						if(got instanceof FileBrowserTab){
							FileBrowserTab fbt=(FileBrowserTab)got;
							if(fbt.fileBrowserPage==null){
								if(fbt.fbi!=null){
									if(fbt.fbi.home.equals(dir)||fbt.fbi.currentDir.equals(dir)){
										ma.gui.TabOnClick.onClick(fbt);
										dismiss();
										break;
									}
								}
							}else{
								FileBrowserPage fbp=fbt.fileBrowserPage;
								if(fbp.home.equals(dir)){
									if(fbp.currentDirectory.equals(dir)){
										if(ma.gui.tfNow==fbt){
											dismiss();
											break;
										}else{
											ma.gui.TabOnClick.onClick(fbt);
											break;
										}
									}else if(ma.gui.tfNow==fbt){
										fbp.letDir(dir);
										dismiss();
										break;
									}else{
										fbp.letDir(dir);
										ma.gui.TabOnClick.onClick(fbt);
										dismiss();
										break;
									}
								}else if(fbp.currentDirectory.equals(dir)){
									if(ma.gui.tfNow==fbt){
										dismiss();
										break;
									}else{
										ma.gui.TabOnClick.onClick(fbt);
										dismiss();
										break;
									}
								}
							}
						}
					}else{
						FileBrowserTab fbt=new FileBrowserTab(ma);
						TabData.FileBrowserInfo fbi=new TabData.FileBrowserInfo();
						fbi.home=dir;
						fbi.currentDir=dir;
						fbt.fbi=fbi;
						fbt.setText(dir.getName());
						fbt.setOnClickListener(ma.gui.TabOnClick);
						ma.gui.tabs.addView(fbt,TabBar.tablp);
						ma.gui.TabOnClick.onClick(fbt);
						dismiss();
						break;
					}
				}
			}else {
				Toast.makeText(ma,"该文件所在的目录不可读",0).show();
			}
		}
	}
}
